package hotspot;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collection;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;

import Vis.User;
import bodyPosition.Position;
import bodyPosition.PositionStorage;

public class SpotMap extends JPanel {

	private static final long serialVersionUID = 5216508989569934931L;
	protected int width, height;
	protected int MAX_X = 650;
	protected int MAX_Z = 2300;
	private HotspotApp hotspotApp;
	protected int range = 80;
	public JPanel container;
	private ArrayList<Position> allPositions = new ArrayList<Position>();
	
	public SpotMap(int w, int h, HotspotApp ha){
		width = w;
		height = h - 45;
		hotspotApp = ha;
		this.setPreferredSize(new Dimension(width, height));
		container = new JPanel(new BorderLayout());
		TitledBorder t = BorderFactory.createTitledBorder("HOTSPOTS");
		t.setTitleJustification(TitledBorder.CENTER);
		container.setBorder(t);
		container.add(this, BorderLayout.CENTER);
		JButton clearButton = new JButton("Clear all");
		clearButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				hotspotApp.clearHotspots();				
			}
		});		
		container.add(clearButton, BorderLayout.NORTH);		
	}
	
	public SpotMap(HotspotApp ha) {
		this(540,320, ha);
	}
	
	@Override
	public void paint(Graphics g) {
		
		//hotspots
		for (Hotspot h : hotspotApp.hotspots){
			g.setColor(Color.BLACK);
			g.fillRect(fitWidth(h.getX()), fitHeight(h.getZ()), 
					getXRange(), getZRange());
			g.drawString(h.getText(), fitWidth(h.getX()) - 10, fitHeight(h.getZ()));
		}
		
		// Active hotspots
		if (hotspotApp.onHotspot != null) {
			g.setColor(Color.RED);
			g.fillRect(fitWidth(hotspotApp.onHotspot.getX()), 
				fitHeight(hotspotApp.onHotspot.getZ()), getXRange(), getZRange());
			g.drawString(hotspotApp.onHotspot.getText(), 
				fitWidth(hotspotApp.onHotspot.getX()) - 10, 
				fitHeight(hotspotApp.onHotspot.getZ()));
		}

		Collection<User> c = hotspotApp.users.getUsers();		
		for (User user : c) {
			PositionStorage ps = user.getPositionStorage();
			for (Position p: ps.getAllPositions()){
				double[] currentPos;
				synchronized(p){
					currentPos = p.getPosition();
				}
				if (p != null) {
					g.setColor(Color.BLUE);
					g.drawRect(fitWidth(currentPos[0]), fitHeight(currentPos[2]),
					   getXRange(), getZRange());
//					g.drawString("User "+user.getID(), 
//							fitWidth(currentPos[0]), //x 
//							   fitHeight(currentPos[2]));
					Position copy = new Position("");
					copy.setPosition(p.getPosition());
					allPositions.add(copy);
				}
			}
		}
//		for (Position pos : allPositions) {
//			g.setColor(Color.BLUE);
//			g.drawOval(fitWidth(pos.getPosition()[0])/2, fitHeight(pos.getPosition()[2])/2,
//			   getXRange()/2, getZRange()/2);
//		}	
	}
	
	private int fitWidth(double w){
		w=w/1000;
		return (int)((w/MAX_X) * width); 
	}
	
	private int fitHeight(double h){
		h=h/1000;
		return (int)((h/MAX_Z) * height);
	}
	
	protected int getXRange(){
		return fitWidth(hotspotApp.xRange*1000);
	}
	
	protected int getZRange(){
		return fitHeight(hotspotApp.zRange*1000);
	}	
}
